2 using System
.Collections
.Generic
;
4 namespace EZCameraShake
6 [AddComponentMenu("EZ Camera Shake/Camera Shaker")]
7 public class CameraShaker
: MonoBehaviour
10 /// The single instance of the CameraShake in the current scene. Do not use if you have multiple instances.
12 public static CameraShaker Instance
;
13 static Dictionary
<string, CameraShaker
> instanceList
= new Dictionary
<string, CameraShaker
>();
16 /// The default position influcence of all shakes created by this shaker.
18 public Vector3 DefaultPosInfluence
= new Vector3(0.15f
, 0.15f
, 0.15f
);
20 /// The default rotation influcence of all shakes created by this shaker.
22 public Vector3 DefaultRotInfluence
= new Vector3(1, 1, 1);
24 public static Vector3 camera_at_hit_time
;
25 public static bool isHit
;
27 Vector3 posAddShake
, rotAddShake
;
29 List
<CameraShakeInstance
> cameraShakeInstances
= new List
<CameraShakeInstance
>();
34 instanceList
.Add(gameObject
.name
, this);
40 posAddShake
= GameObject
.FindGameObjectWithTag("CameraDestination").transform
.localPosition
;
41 rotAddShake
= Vector3
.zero
;
44 for (int i
= 0; i
< cameraShakeInstances
.Count
; i
++)
46 if (i
>= cameraShakeInstances
.Count
)
49 CameraShakeInstance c
= cameraShakeInstances
[i
];
51 if (c
.CurrentState
== CameraShakeState
.Inactive
&& c
.DeleteOnInactive
)
53 cameraShakeInstances
.RemoveAt(i
);
56 else if (c
.CurrentState
!= CameraShakeState
.Inactive
)
58 posAddShake
+= CameraUtilities
.MultiplyVectors(c
.UpdateShake(), c
.PositionInfluence
);
59 rotAddShake
+= CameraUtilities
.MultiplyVectors(c
.UpdateShake(), c
.RotationInfluence
);
64 transform
.localPosition
= posAddShake
;
65 transform
.localEulerAngles
= rotAddShake
;
67 if (cameraShakeInstances
.Count
== 0)
72 /// Gets the CameraShaker with the given name, if it exists.
74 /// <param name="name">The name of the camera shaker instance.</param>
75 /// <returns></returns>
76 public static CameraShaker
GetInstance(string name
)
80 if (instanceList
.TryGetValue(name
, out c
))
83 Debug
.LogError("CameraShake " + name
+ " not found!");
89 /// Starts a shake using the given preset.
91 /// <param name="shake">The preset to use.</param>
92 /// <returns>A CameraShakeInstance that can be used to alter the shake's properties.</returns>
93 public CameraShakeInstance
Shake(CameraShakeInstance shake
)
95 cameraShakeInstances
.Add(shake
);
100 /// Shake the camera once, fading in and out over a specified durations.
102 /// <param name="magnitude">The intensity of the shake.</param>
103 /// <param name="roughness">Roughness of the shake. Lower values are smoother, higher values are more jarring.</param>
104 /// <param name="fadeInTime">How long to fade in the shake, in seconds.</param>
105 /// <param name="fadeOutTime">How long to fade out the shake, in seconds.</param>
106 /// <returns>A CameraShakeInstance that can be used to alter the shake's properties.</returns>
107 public CameraShakeInstance
ShakeOnce(float magnitude
, float roughness
, float fadeInTime
, float fadeOutTime
)
109 CameraShakeInstance shake
= new CameraShakeInstance(magnitude
, roughness
, fadeInTime
, fadeOutTime
);
113 shake
.PositionInfluence
= DefaultPosInfluence
;
114 shake
.RotationInfluence
= DefaultRotInfluence
;
115 cameraShakeInstances
.Add(shake
);
121 /// Shake the camera once, fading in and out over a specified durations.
123 /// <param name="magnitude">The intensity of the shake.</param>
124 /// <param name="roughness">Roughness of the shake. Lower values are smoother, higher values are more jarring.</param>
125 /// <param name="fadeInTime">How long to fade in the shake, in seconds.</param>
126 /// <param name="fadeOutTime">How long to fade out the shake, in seconds.</param>
127 /// <param name="posInfluence">How much this shake influences position.</param>
128 /// <param name="rotInfluence">How much this shake influences rotation.</param>
129 /// <returns>A CameraShakeInstance that can be used to alter the shake's properties.</returns>
130 public CameraShakeInstance
ShakeOnce(float magnitude
, float roughness
, float fadeInTime
, float fadeOutTime
, Vector3 posInfluence
, Vector3 rotInfluence
)
132 CameraShakeInstance shake
= new CameraShakeInstance(magnitude
, roughness
, fadeInTime
, fadeOutTime
);
133 shake
.PositionInfluence
= posInfluence
;
134 shake
.RotationInfluence
= rotInfluence
;
135 cameraShakeInstances
.Add(shake
);
138 camera_at_hit_time
= GameObject
.FindGameObjectWithTag ("MainCamera").transform
.position
;
144 /// Start shaking the camera.
146 /// <param name="magnitude">The intensity of the shake.</param>
147 /// <param name="roughness">Roughness of the shake. Lower values are smoother, higher values are more jarring.</param>
148 /// <param name="fadeInTime">How long to fade in the shake, in seconds.</param>
149 /// <returns>A CameraShakeInstance that can be used to alter the shake's properties.</returns>
150 public CameraShakeInstance
StartShake(float magnitude
, float roughness
, float fadeInTime
)
152 CameraShakeInstance shake
= new CameraShakeInstance(magnitude
, roughness
);
153 shake
.PositionInfluence
= DefaultPosInfluence
;
154 shake
.RotationInfluence
= DefaultRotInfluence
;
155 shake
.StartFadeIn(fadeInTime
);
156 cameraShakeInstances
.Add(shake
);
161 /// Start shaking the camera.
163 /// <param name="magnitude">The intensity of the shake.</param>
164 /// <param name="roughness">Roughness of the shake. Lower values are smoother, higher values are more jarring.</param>
165 /// <param name="fadeInTime">How long to fade in the shake, in seconds.</param>
166 /// <param name="posInfluence">How much this shake influences position.</param>
167 /// <param name="rotInfluence">How much this shake influences rotation.</param>
168 /// <returns>A CameraShakeInstance that can be used to alter the shake's properties.</returns>
169 public CameraShakeInstance
StartShake(float magnitude
, float roughness
, float fadeInTime
, Vector3 posInfluence
, Vector3 rotInfluence
)
171 CameraShakeInstance shake
= new CameraShakeInstance(magnitude
, roughness
);
172 shake
.PositionInfluence
= posInfluence
;
173 shake
.RotationInfluence
= rotInfluence
;
174 shake
.StartFadeIn(fadeInTime
);
175 cameraShakeInstances
.Add(shake
);
180 /// Gets a copy of the list of current camera shake instances.
182 public List
<CameraShakeInstance
> ShakeInstances
183 { get { return new List<CameraShakeInstance>(cameraShakeInstances); }
}
187 instanceList
.Remove(gameObject
.name
);